#SSBM .dat file import module v1.4a
def inf(support,im_types,ex_types,C_mthds): #plugin information
    support.append("*.dat") #supported import type
    im_types.append(('Melee','*.dat')) #individual import type
    #ex_types.append(('Melee','*.dat')) #not yet
    C_mthds.append(['dat','Melee','im_SSBM','ex_SSBM']) #conversion method info/key
    #([ file_type , button_text/key, imbtn_name, exbtn_name ])

#import extensions:
import GlobalTMP as G #global functions for conversion
import NtdoCmn as NC #common nintendo formats
import Cmn as C #common functions

verticesList = []
normalsList = []
uvsList0 = []

def impt(imfl,imkey,debug): #import function
    if imfl.split(".")[-1] == "dat" and imkey == 'Melee': #check extension + safety check (requitred)
        fr = open(imfl,'rb') #open dat file
        G.setWrite() #global.TMP to write
        NC.setFile(fr) #set the import file for NtdoCmn

        #settings
        glvcs = 1
        #trvcs = 0

        def POBJ(Offset): #objects
            fr.seek(Offset+32, 0)
            
            unk = C.ru32()
            next = C.ru32()
            attr = C.ru32()
            flags = fr.read(2)
            DL_size = C.ru16()*32
            DL = C.ru32()
            weights = C.ru32()
            
            def attribute(offset):
                fr.seek(offset+32, 0)
                
                FP_idx = C.ru32()
                idx_type = C.ru32()
                cnt = C.ru32()
                dtype = C.ru32()
                mult = C.ru8()
                unk = C.ru8()
                stride = C.ru16()
                offset = C.ru32()
                
                return FP_idx,idx_type,cnt,dtype,mult,unk,stride,offset
            
            #data inputs and formatting
            vinput,vstride,vdtype,vdiv = 0,0,0,0
            ninput,nstride,ndtype,ndiv = 0,0,0,0
            u0input,u0stride,u0dtype,u0div = 0,0,0,0
            CP_List = [0]*21 #define the CP_list
            
            current = 0
            stop = 0
            while not stop:
                CP_idx,idx_type,cnt,dtype,div,unk,stride,offset = attribute(attr+(current*24))
                if CP_idx != 255:
                    CP_List[CP_idx] = idx_type #set the CP_index type

                    if CP_idx == 9: vinput,vstride,vdtype,vdiv = offset,stride,dtype,div #set vert attributes
                    if CP_idx == 10: ninput,nstride,ndtype,ndiv = offset,stride,dtype,div #set norm attributes
                    if CP_idx == 13: u0input,u0stride,u0dtype,u0div = offset,stride,dtype,div #set uv0 attributes
                        
                    current += 1
                else: stop = 1
                
            prmMatrices, FacePoints, Primitive_data = NC.getDisplayList( CP_List, (DL+32), DL_size ) #read the DisplayList
            
            #set the length of the lists before parsing the facepoints
            for Facepoint in FacePoints: #will get to the normals and uv's
                #print Facepoint
                global trvcs
                if Facepoint[0] != '': trvcs = 1
                else: trvcs = 0
                if Facepoint[9] != '':
                    if len(verticesList) < (Facepoint[9]+1):
                        while len(verticesList) < (Facepoint[9]+1): verticesList.append(0)
                    verticesList[Facepoint[9]] = NC.getNVector(vdtype,vdiv,(vinput+32+(vstride*Facepoint[9])),vstride,1)[0]
                if Facepoint[10] != '':
                    if len(normalsList) < (Facepoint[10]+1):
                        while len(normalsList) < (Facepoint[10]+1): normalsList.append(0)
                    normalsList[Facepoint[10]] = NC.getNVector(ndtype,ndiv,(ninput+32+(nstride*Facepoint[10])),nstride,1)[0]
                if Facepoint[13] != '':
                    if len(uvsList0) < (Facepoint[13]+1):
                        while len(uvsList0) < (Facepoint[13]+1): uvsList0.append(0)
                    uvsList0[Facepoint[13]] = NC.getNVector(u0dtype,u0div,(u0input+32+(u0stride*Facepoint[13])),u0stride,0)[0]
                    
            PrimitivesList, Colors, FP_Matrices = NC.DList2PList( FacePoints, Primitive_data ) #finish DisplayList conversion
            #write the data to the TMP:
            G.setObject( Offset+32,str(Offset+32),'',[],PrimitivesList,[])

            #G.setPrimitives(PrimitivesList,Offset+32)
            

            if next != 0: POBJ(next) #next object

            print 'object:' + str(Offset)

        def DOBJ(offset):
            fr.seek(offset+32, 0)
            
            unk = fr.read(4)
            next = C.ru32()
            material = C.ru32()
            polygon = C.ru32()

            if next != 0: DOBJ(next)
            #if material != 0: MOBJ(material)
            if polygon != 0: POBJ(polygon)

            print 'data' + str(offset)

        def JOBJ(offset):
            fr.seek(offset+32, 0)
            
            unk1 = fr.read(4)
            flags = fr.read(4)
            child = C.ru32()
            next = C.ru32()
            data = C.ru32()
            RX,RY,RZ = C.rf,C.rf,C.rf
            SX,SY,SZ = C.rf,C.rf,C.rf
            LX,LY,LZ = C.rf,C.rf,C.rf
            inverse = C.ru32()
            unk2 = fr.read(4)
            
            if child != 0: JOBJ(child)
            if next != 0: JOBJ(next)
            if data != 0: DOBJ(data)

            #inverse(inverse)

            print 'bone: ' + str(offset+32)
            
        #<<header>>
        file_size = C.ru32()
        offset_tbl = C.ru32()
        num_offsets = C.ru32()
        num_bases = C.ru32()
        num_refs = C.ru32()
        tmp1 = C.ru32() #001B or ....
        unk1 = C.ru32()
        unk2 = C.ru32()
        #<</header>>

        #build the node list
        nodes = []
        
        current_base = 0
        while current_base < num_bases:
            fr.seek(offset_tbl+(num_offsets*4)+(current_base*8)+32, 0)
            data_offset = C.ru32()
            string_offset = C.ru32()
            fr.seek(offset_tbl+(num_offsets*4)+((num_bases+num_refs)*8)+string_offset+32, 0)
            chr_lst,stop = [],0
            while stop == 0:
                char = fr.read(1)
                if char == chr(0) or char == '': stop = 1
                else: chr_lst.append(char)
            nodes.append([data_offset,''.join(chr_lst)])
            current_base += 1

        #print nodes

        for node in nodes:
            print node
            if (node[1]).split('_')[2] == 'joint': JOBJ(node[0])

        #decode the heirarchy for each node
        #print verticesList
        G.setVector(verticesList,0,0,'global',0)
        G.setVector(normalsList,1,0,'global',0)
        G.setVector(uvsList0,2,0,'global',0)

        trvcs = 0 #small hack
        #set specs
        settings = []
        if glvcs: settings.append('vecs_global')
        if trvcs: settings.append('vecs_transf')

        G.setSpecs(settings)

        fr.close()
        G.closeWrite()
        
def expt(exfl,exSC,debug): #export function
    #to be used later
    pass
